//
const ssr = require('../line-of-sight/namespace/SSRLoSNamespace');

cc.Class({
    extends: cc.Component,

    properties: {
    	robot: {
    		default: null,
    		type: cc.Node
    	},
    	obstaclesGroup: {
            default         : null,
            type            : cc.Node
        },
        polylineColliderObstaclePrefab: {
            default         : null,
            type            : cc.Prefab  
        },
        polygonColliderObstaclePrefab: {
            default         : null,
            type            : cc.Prefab  
        },
    },

    start: function () {
        this.camera = cc.find("Main Camera", this.node).getComponent(cc.Camera);
        this.robotObject = this.robot.getComponent("Robot");
        this.robotLoSComponent = this.robot.getComponent("SSRLoSComponentCore");
        this.robotLoSCore = this.robotLoSComponent.getLoSCore();
        if (this.obstaclesGroup) {
            var children = this.obstaclesGroup.getChildren();
            for (var i = 0, l = children.length; i < l; i ++) {
                var polygonColliderObstacleNode = children[i];
                var polygonColliderObstacle = polygonColliderObstacleNode.getComponent("PolygonColliderObstacle");
                this.robotLoSCore.addObstacle(polygonColliderObstacleNode, polygonColliderObstacle.getVertexArray());
            }
        }
        this.node.getChildByName("FreeDrawing").on('FreeDrawingComponent_onTouchEnd', this.onFreeDrawingComponentTouchEnd.bind(this));
        this.node.getChildByName("PolygonDrawing").on('PolygonDrawingComponent_onTouchEnd', this.onPolygonDrawingComponentTouchEnd.bind(this));
    },
    onFreeDrawingComponentTouchEnd:function(data) {
        var node = cc.instantiate(this.polylineColliderObstaclePrefab);
        node.parent = this.node;
        node.getComponent("PolylineColliderObstacle").updateVertexArray(data);
        this.robotLoSCore.addObstacle(node, data, false);
    },
    onPolygonDrawingComponentTouchEnd:function(data, isPolygon) {
        var node = cc.instantiate(this.polygonColliderObstaclePrefab);
        node.parent = this.node;
        node.getComponent("PolygonColliderObstacle").updateVertexArray(data);
        this.robotLoSCore.addObstacle(node, data, isPolygon);
    },
    // called every frame
    update: function (dt) {
        if (this.robotObject.useCamera) {
            this.camera.node.position = this.robot.position;
        }
        if (this.robotObject.isForceLoSUpdate) {
            this.robotLoSCore.enableForceUpdate();
            this.robotLoSCore.setDirtyFlag(ssr.LoS.Constant.DIRTY_FLAGS.BOUNDARY);
            this.robotLoSCore.setDirtyFlag(ssr.LoS.Constant.DIRTY_FLAGS.CULLING);
        }
        //
        var isUpdated = this.robotLoSComponent.updateSightNode();
        if (isUpdated || this.robotObject.isForceLoSUpdate) {
            this.node.getComponent("LoSRenderLayer").updateRender(this.robotObject.isForceLoSUpdate)
            this.node.getComponent("FloatingMenu").updateDebugDraw()
        }
        this.robotLoSCore.disableForceUpdate();
        this.robotObject.isForceLoSUpdate = false;
    }
});
